/*
 * qspi.c
 *
 *  Created on: Apr 3, 2013
 *      Author: bfakhri
 */

void qspi_init(){
	//This function sets up QSPI so we can transmit data to LEDarray
	//Pin 0 connects to SPI0_CLK on LEDArray
	//Pin 2 connects to SPI0_MOSI on LEDArray
	
	//Sets the pins to QS functionality (Primary function)
	//Pin 0 
	MCF_GPIO_PQSPAR &= ~MCF_GPIO_PQSPAR_PQSPAR0(3);
	MCF_GPIO_PQSPAR |= ~MCF_GPIO_PQSPAR_PQSPAR0(1);
	//Pin 2
	MCF_GPIO_PQSPAR &= ~MCF_GPIO_PQSPAR_PQSPAR2(3);
	MCF_GPIO_PQSPAR |= ~MCF_GPIO_PQSPAR_PQSPAR2(1);
	
	//Sets MSTR
	MCF_QSPI_QMR |= MCF_QSPI_QMR_MSTR;
	//Sets bits per transfer to 8 (is 8 right?)
	MCF_QSPI_QMR &= ~MCF_QSPI_QMR_BITS(0xF);	// Clears
	MCF_QSPI_QMR |= MCF_QSPI_QMR_BITS(8);		// Sets to value
	//Sets Polarity and Phase
	MCF_QSPI_QMR |= MCF_QSPI_QMR_CPOL;
	MCF_QSPI_QMR &= ~MCF_QSPI_QMR_CPHA;
	//Sets BAUD
	MCF_QSPI_QMR &= ~MCF_QSPI_QMR_BAUD(FF);		// Clears
	MCF_QSPI_QMR |= MCF_QSPI_QMR_BAUD(40);		// Sets to value - assumes 1Mbps baud rate
	
	//Disable QSPI transfer
	MCF_QSPI_QDLYR &= ~MCF_QSPI_QDLYR_SPE;		// Clears SPE
	//Sets QCD
	MCF_QSPI_QDLYR &= ~MCF_QSPI_QDLYR_QCD(0x7F);// Clears QCD (no delay)
	//Sets DTL
	MCF_QSPI_QDLYR &= MCF_QSPI_QDLYR_DTL(FF);	// Clears DTL (no delay)
	
	//Sets interrupt register QIR
	MCF_QSPI_QIR &= ~(MCF_QSPI_QIR_SPIFE | MCF_QSPI_QIR_ABRTE | MCF_QSPI_QIR_WCEFE | MCF_QSPI_QIR_ABRTL | MCF_QSPI_QIR_ABRTB | MCF_QSPI_QIR_WCEFB); 
	MCF_QSPI_QIR |= (MCF_QSPI_QIR_SPIF |	MCF_QSPI_QIR_ABRT |	MCF_QSPI_QIR_WCEF);
	
	//Sets Wrap Register 
	MCF_QSPI_QWR &= ~(MCF_QSPI_QWR_CSIV | MCF_QSPI_QWR_WRTO | MCF_QSPI_QWR_WREN | MCF_QSPI_QWR_HALT);
	MCF_QSPI_QWR &= ~MCF_QSPI_QWR_ENDQP(0xF); 
	MCF_QSPI_QWR &= ~MCF_QSPI_QWR_NEWQP(0xF);
}